﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;

namespace Customer_Profile.eCard
{
    public partial class ProductAdd : System.Web.UI.Page
    {
        string prod_code = "";
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (Request["prod_code"] != null)
            {
                prod_code = Request["prod_code"].ToString();
            }
            if (!Page.IsPostBack)
            {
                Comm.BindDll(ref dropPeriodType, "TermUnit", false, "");
                if (!prod_code.Equals(""))
                {
                    bindPage();
                    btnSave.Enabled = false;
                }
                else
                {
                    btnEdit.Enabled=false;
                }
            }
        }

        private void bindPage()
        {
            try
            {
                DataSet ds = ProductDao.getProductbyProdcode(prod_code);
                txtProdCode.Value = prod_code;
                txtProdCode.Disabled = true;
                txtProdName.Value = ds.Tables[0].Rows[0]["procduct_name"].ToString();
                txtDiscount.Value = ds.Tables[0].Rows[0]["sale_discount"].ToString();
                txtpercent.Value = ds.Tables[0].Rows[0]["commission_percent"].ToString();
                txtSettlePercent.Value = ds.Tables[0].Rows[0]["settle_percent"].ToString();
                txtyeild.Value = ds.Tables[0].Rows[0]["annual_yeild"].ToString();
                txt_costing.Value = ds.Tables[0].Rows[0]["sale_costing"].ToString();
                txtInitAmount.Value = ds.Tables[0].Rows[0]["initial_amout"].ToString();
                txtPeriod.Value = ds.Tables[0].Rows[0]["period"].ToString();
                dropPeriodType.Items.FindByValue(ds.Tables[0].Rows[0]["unit"].ToString().Trim()).Selected = true;
                dropIf_used.Items.FindByValue(ds.Tables[0].Rows[0]["in_use"].ToString().Trim()).Selected = true;
                if (ds.Tables[0].Rows[0]["if_appointDate"].ToString().Equals("Y"))
                {
                    chk_ifappointDate.Checked = true;
                }
                else
                {
                    chk_ifappointDate.Checked = false;
                }
                txtcreatedatebegin.Text = ds.Tables[0].Rows[0]["start_date"].ToString();
                txtcreatedateend.Text = ds.Tables[0].Rows[0]["end_date"].ToString();
                txt_convertratio.Value = ds.Tables[0].Rows[0]["convert_ratio"].ToString();
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("ProductAdd_bindPage", ex.ToString());
                Response.Write(ex.ToString());
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string procduct_code = txtProdCode.Value.Trim();
                string procduct_name = txtProdName.Value.Trim();
                string sale_discount = txtDiscount.Value.Trim();
                string commission_percent = txtpercent.Value.Trim();
                string settle_percent = txtSettlePercent.Value.Trim();
                string annual_yeild = txtyeild.Value.Trim();
                string sale_costing = txt_costing.Value.Trim();
                string initial_amout = txtInitAmount.Value.Trim();
                string period = txtPeriod.Value.Trim();
                string unit = dropPeriodType.SelectedItem.Value;
                string unitRemark = dropPeriodType.SelectedItem.Text;
                string in_use = dropIf_used.SelectedItem.Value;
                string if_appointDate = chk_ifappointDate.Checked ? "Y" : "N";
                string start_date = txtcreatedatebegin.Text.Trim();
                string end_date = txtcreatedateend.Text.Trim();
                string convert_ratio = txt_convertratio.Value.Trim();

                bool bok = ProductDao.ProductAdd(procduct_code, procduct_name, sale_discount, commission_percent,settle_percent, annual_yeild, sale_costing, initial_amout, period, unit, unitRemark, in_use, if_appointDate, start_date, end_date, convert_ratio);
                if (bok)                    
                    KVB.Common.JavaScript.ViewMsgGoUrl("ProdList.aspx", "添加成功");
                else
                    KVB.Common.JavaScript.ViewAlertMsg("添加失败！");
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("ProductAdd.btnSave_Click", ex.ToString());                
                KVB.Common.JavaScript.ViewAlertMsg(ex.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProdList.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                string procduct_code = txtProdCode.Value.Trim();
                string procduct_name = txtProdName.Value.Trim();
                string sale_discount = txtDiscount.Value.Trim();
                string commission_percent = txtpercent.Value.Trim();
                string settle_percent = txtSettlePercent.Value.Trim();
                string annual_yeild = txtyeild.Value.Trim();
                string sale_costing = txt_costing.Value.Trim();
                string initial_amout = txtInitAmount.Value.Trim();
                string period = txtPeriod.Value.Trim();
                string unit = dropPeriodType.SelectedItem.Value;
                string unitRemark = dropPeriodType.SelectedItem.Text;
                string in_use = dropIf_used.SelectedItem.Value;
                string if_appointDate = chk_ifappointDate.Checked ? "Y" : "N";
                string start_date = txtcreatedatebegin.Text.Trim();
                string end_date = txtcreatedateend.Text.Trim();
                string convert_ratio = txt_convertratio.Value.Trim();                

                bool bok = ProductDao.ProductUpdate(procduct_code, procduct_name, sale_discount, commission_percent,settle_percent, annual_yeild, sale_costing, initial_amout, period, unit, unitRemark, in_use, if_appointDate, start_date, end_date, convert_ratio);
                if (bok)
                    Response.Write("<script language=javascript>alert('修改成功');</script>");
                else
                    Response.Write("<script language=javascript>alert('修改失败');</script>");
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("addcflow.btnEdit_Click", ex.ToString());
                KVB.Common.JavaScript.ViewAlertMsg(ex.ToString());
            }
        }
    }
}